<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

        var $css= array('asset/js/jquery.mobile/jquery.mobile-1.4.2.css');
        //var $img= array('images/favicon.ico');
        //var $txt= array('hymans.txt');
        var $js= array('asset/js/jquery.js', 'asset/js/jquery-ui/js/jquery-ui-1.10.4.custom.js', 'asset/js/jquery.mobile/jquery.mobile-1.4.2.js');

        public function __construct() {
 	    parent::__construct();
	    $this->load->helper('url');
	    $this->load->model('user','',TRUE);
	    $this->load->model('name_model');

	}

	public function index()
	{
	    if($this->session->userdata('logged_in'))
	    {
		$session_data = $this->session->userdata('logged_in');
		$data['fb_username'] = $session_data['fb_username'];

	        $header['css'] = $this->css;
	        $header['js'] = $this->js;
		
		$this->load->view('header', $header);
		$this->load->view('script'); 
		$this->load->view('new_feed', $data);
		$this->load->view('footer');
	    }
	    else
	    {
		redirect('home/login', 'refresh');
	    }
	}

	public function page()
	{
	    if($this->session->userdata('logged_in'))
	    {
		$session_data = $this->session->userdata('logged_in');
		$data['fb_username'] = $session_data['fb_username'];
		$data['fb'] = $this->name_model->filter_fb($session_data['fb_username']);

		$data['post_wall'] = $this->name_model->filter_post_wall($data['fb'][0]['fb_id']);
		$data['post_newfeed'] = $this->name_model->all_newfeed();
		$data['all_user'] = $this->name_model->all_user();

		for($i=0;$i<count($data['post_newfeed']);$i++)
		{
		    $data['post_newfeed'][$i]['fb_username_poster'] = $this->name_model->get_username($data['post_newfeed'][$i]['fb_id_poster']);
		    $data['post_newfeed'][$i]['fb_pic_poster'] = $this->name_model->get_fb_pic($data['post_newfeed'][$i]['fb_id_poster']);
		    $data['post_newfeed'][$i]['fb_username_wall'] = $this->name_model->get_username($data['post_newfeed'][$i]['fb_id_wall']);
		    $data['post_newfeed'][$i]['fb_pic_wall'] = $this->name_model->get_fb_pic($data['post_newfeed'][$i]['fb_id_wall']);

		    $data['post_newfeed'][$i]['total_like'] = count($this->name_model->filter_like($data['post_newfeed'][$i]['post_id']));
		    $data['post_newfeed'][$i]['liked'] = $this->name_model->filter_like_fb($data['post_newfeed'][$i]['post_id'], $data['fb'][0]['fb_id']);
		}

		for($i=0;$i<count($data['post_wall']);$i++)
		{    
		    $data['post_wall'][$i]['fb_username_poster'] = $this->name_model->get_username($data['post_wall'][$i]['fb_id_poster']);
		    $data['post_wall'][$i]['fb_pic_poster'] = $this->name_model->get_fb_pic($data['post_wall'][$i]['fb_id_poster']);
		    $data['post_wall'][$i]['fb_username_wall'] = $this->name_model->get_username($data['post_wall'][$i]['fb_id_wall']);
		    $data['post_wall'][$i]['fb_pic_wall'] = $this->name_model->get_fb_pic($data['post_wall'][$i]['fb_id_wall']);

		    $data['post_wall'][$i]['total_like'] = count($this->name_model->filter_like($data['post_wall'][$i]['post_id']));
		    $data['post_wall'][$i]['liked'] = $this->name_model->filter_like_fb($data['post_wall'][$i]['post_id'], $data['fb'][0]['fb_id']);

		    $data['post_wall'][$i]['comment'] = $this->name_model->filter_comment($data['post_wall'][$i]['post_id']);
		    for($j=0;$j<count($data['post_wall'][$i]['comment']);$j++)
		    {
			$data['post_wall'][$i]['comment'][$j]['fb_username_com'] = $this->name_model->get_username($data['post_wall'][$i]['comment'][$j]['fb_id_com']);
			$data['post_wall'][$i]['comment'][$j]['fb_pic_com'] = $this->name_model->get_fb_pic($data['post_wall'][$i]['comment'][$j]['fb_id_com']);
		    }
		}

	        $header['css'] = $this->css;
	        $header['js'] = $this->js;
		
		$this->load->view('header', $header);
		$this->load->view('script', $data); 
		$this->load->view('page');
		$this->load->view('footer');
	    }
	    else
	    {
		redirect('home/login', 'refresh');
	    }
	}

	public function new_feed()
	{
	    if($this->session->userdata('logged_in'))
	    {
		$session_data = $this->session->userdata('logged_in');
		$data['fb_username'] = $session_data['fb_username'];
		$data['fb'] = $this->name_model->filter_fb($data['fb_username']);

		$data['post_wall'] = $this->name_model->filter_post_wall($data['fb'][0]['fb_id']);
		$data['post_newfeed'] = $this->name_model->all_newfeed();
		$data['all_user'] = $this->name_model->all_user();
		
		for($i=0;$i<count($data['post_newfeed']);$i++)
		{
		    $data['post_newfeed'][$i]['fb_username_poster'] = $this->name_model->get_username($data['post_newfeed'][$i]['fb_id_poster']);
		    $data['post_newfeed'][$i]['fb_pic_poster'] = $this->name_model->get_fb_pic($data['post_newfeed'][$i]['fb_id_poster']);
		    $data['post_newfeed'][$i]['fb_username_wall'] = $this->name_model->get_username($data['post_newfeed'][$i]['fb_id_wall']);
		    $data['post_newfeed'][$i]['fb_pic_wall'] = $this->name_model->get_fb_pic($data['post_newfeed'][$i]['fb_id_wall']);

		    $data['post_newfeed'][$i]['total_like'] = count($this->name_model->filter_like($data['post_newfeed'][$i]['post_id']));
		    $data['post_newfeed'][$i]['liked'] = $this->name_model->filter_like_fb($data['post_newfeed'][$i]['post_id'], $data['fb'][0]['fb_id']);
		}

	        $header['css'] = $this->css;
	        $header['js'] = $this->js;
		
		$this->load->view('header', $header);
		$this->load->view('script', $data); 
		$this->load->view('new_feed');
		$this->load->view('footer');
	    }
	    else
	    {
		redirect('home/login', 'refresh');
	    }
	}

	public function wall()
	{
	    if($this->session->userdata('logged_in'))
	    {
		$session_data = $this->session->userdata('logged_in');
		$data['fb_username'] = $session_data['fb_username'];
		$data['fb'] = $this->name_model->filter_fb($data['fb_username']);

		$data['post_wall'] = $this->name_model->filter_post_wall($data['fb'][0]['fb_id']);
		$data['all_user'] = $this->name_model->all_user();

		for($i=0;$i<count($data['post_wall']);$i++)
		{    
		    $data['post_wall'][$i]['fb_username_poster'] = $this->name_model->get_username($data['post_wall'][$i]['fb_id_poster']);
		    $data['post_wall'][$i]['fb_pic_poster'] = $this->name_model->get_fb_pic($data['post_wall'][$i]['fb_id_poster']);
		    $data['post_wall'][$i]['fb_username_wall'] = $this->name_model->get_username($data['post_wall'][$i]['fb_id_wall']);
		    $data['post_wall'][$i]['fb_pic_wall'] = $this->name_model->get_fb_pic($data['post_wall'][$i]['fb_id_wall']);

		    $data['post_wall'][$i]['total_like'] = count($this->name_model->filter_like($data['post_wall'][$i]['post_id']));
		    $data['post_wall'][$i]['liked'] = $this->name_model->filter_like_fb($data['post_wall'][$i]['post_id'], $data['fb'][0]['fb_id']);

		    $data['post_wall'][$i]['comment'] = $this->name_model->filter_comment($data['post_wall'][$i]['post_id']);
		    for($j=0;$j<count($data['post_wall'][$i]['comment']);$j++)
		    {
			$data['post_wall'][$i]['comment'][$j]['fb_username_com'] = $this->name_model->get_username($data['post_wall'][$i]['comment'][$j]['fb_id_com']);
			$data['post_wall'][$i]['comment'][$j]['fb_pic_com'] = $this->name_model->get_fb_pic($data['post_wall'][$i]['comment'][$j]['fb_id_com']);
		    }
		}

	        $header['css'] = $this->css;
	        $header['js'] = $this->js;

		$this->load->view('header', $header);
		$this->load->view('script', $data); 
		$this->load->view('wall');
		$this->load->view('footer');
	    }
	    else
	    {
			redirect('home/login', 'refresh');
	    }
	}
        public function login()
        {
	    $this->load->helper(array('form'));
	    $this->load->library('form_validation');
	    
	    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
	    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');

	    if($this->form_validation->run() == 1)
	    {

		if($this->session->userdata('logged_in'))
		{
		    redirect('home/page');
		}
		else
		{
		    redirect('home/login', 'refresh');
		}
	    }
	    else
	    {
		$header['css'] = $this->css;
		$header['js'] = $this->js;
		
		$this->load->view('header', $header);
		$this->load->view('login_view');
		$this->load->view('footer');
	    }
	}

        function logout()
        {
	    $this->session->unset_userdata('logged_in');
	    session_destroy();


	    $this->load->helper(array('form'));
	    $this->load->library('form_validation');
	    
	    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
	    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
	   
	    if($this->form_validation->run() == '1')
	    {
		
		if($this->session->userdata('logged_in'))
		{
		    $session_data = $this->session->userdata('logged_in');
		    $data['username'] = $session_data['username'];
		    
		    $header['css'] = $this->css;
		    $header['js'] = $this->js;
		    
		    $this->load->view('header', $header);
		    $this->load->view('script'); 
		    $this->load->view('index');
		    $this->load->view('footer');
		}
		else
		{
		    redirect('home/login', 'refresh');
		}
	    }
	    else
	    {
		$header['css'] = $this->css;
		$header['js'] = $this->js;
		
		$this->load->view('header', $header);
		$this->load->view('script'); 
		$this->load->view('login_view');
		$this->load->view('footer');
	    }

	}

        function check_database($password)
        {
	    $username = $this->input->post('username');
	    
	    $result = $this->user->login($username, $password);
	    if(count($result)>0)
	    {
		$sess_array = array();
		$sess_array = array(
		    'fb_id' => $result[0]['fb_id'],
		    'fb_username' => $result[0]['fb_username']
		);
		$this->session->set_userdata('logged_in', $sess_array);
		return TRUE;
		
	    }
	    else
	    {
		$this->form_validation->set_message('check_database', 'Invalid username or password');
		return FALSE;
	    }
	}

/////////////////////////////////////////post//////////////////////////////////////////////////////


        function ajax_render_newfeed()
        {
	    if($_POST)
	    {
		$data['post_id'] = $_POST['post_id'];
		$data['profile_pic'] = $_POST['profile_pic'];
		$data['profile_name'] = $_POST['profile_name'];
		$data['post_content'] = $_POST['post_content'];
		$data['post_pic'] = $_POST['post_pic'];
		$data['fb_id_poster'] = $_POST['fb_id_poster'];
		$data['fb_id_wall'] = $_POST['fb_id_wall'];
		$data['timestamp'] = $_POST['timestamp'];
		
		$this->load->view('ajax/ajax_render_newfeed', $data);
	    }
	    else
	    {
		echo "FAIL";
	    }
	}

        function ajax_post()
        {   
	    if($_POST)
	    {
		$post_content = $_POST['post_content'];
		$post_pic = $_POST['post_pic'];
		$fb_id_poster = $_POST['fb_id_poster'];
		$fb_id_wall = $_POST['fb_id_wall'];
		$data = array(
		    'post_content'=>$post_content,
		    'post_pic'=>$post_pic,
		    'fb_id_poster'=>$fb_id_poster,
		    'fb_id_wall'=>$fb_id_wall,
		);
		$this->name_model->insert_post($data);
		$post_id = $this->name_model->get_new_post_data($fb_id_poster);
		echo $post_id['0']['post_id'];
	    }
	    else
	    {
		echo "FAIL";
	    }
	}

        function ajax_insert_likep()
        {
	    if($_POST)
	    {
		$post_id = $_POST['post_id'];
		$fb_id = $_POST['fb_id'];
		$data = array(
		    'post_id'=>$post_id,
		    'fb_id'=>$fb_id,
		);
		$this->name_model->insert_likep($data);
	    }
        }
        function ajax_delete_likep()
        {
	    if($_POST)
	    {
		$post_id = $_POST['post_id'];
		$fb_id = $_POST['fb_id'];
		$data = array(
		    'post_id'=>$post_id,
		    'fb_id'=>$fb_id,
		);
		$this->name_model->delete_likep($data);
	    }
        }
	
	function ajax_update_likep()
	{
	     if($_POST)
	     {
		$post_id = $_POST['post_id'];	
		$likeNo = $this->name_model->filter_like($post_id);
		echo count($likeNo);
	     }	
	}

/////////////////////////////////////////post//////////////////////////////////////////////////////

/////////////////////////////////////////comment//////////////////////////////////////////////////////

        function ajax_loadcomment()
        {
	    $session_data = $this->session->userdata('logged_in');
	    $data['fb_username'] = $session_data['fb_username'];
	    $data['fb'] = $this->name_model->filter_fb($data['fb_username']);
	    
	    if($_POST['post_id']){
		
		$data['post_wall'] = $this->name_model->filter_post($_POST['post_id']);
		
		$data['post_wall'][0]['fb_username_poster'] = $this->name_model->get_username($data['post_wall'][0]['fb_id_poster']);
		$data['post_wall'][0]['fb_pic_poster'] = $this->name_model->get_fb_pic($data['post_wall'][0]['fb_id_poster']);
		$data['post_wall'][0]['fb_username_wall'] = $this->name_model->get_username($data['post_wall'][0]['fb_id_wall']);
		$data['post_wall'][0]['fb_pic_wall'] = $this->name_model->get_fb_pic($data['post_wall'][0]['fb_id_wall']);
		$data['post_wall'][0]['total_like'] = count($this->name_model->filter_like($data['post_wall'][0]['post_id']));
		$data['post_wall'][0]['liked'] = $this->name_model->filter_like_fb($data['post_wall'][0]['post_id'], $data['fb'][0]['fb_id']);
		
		$data['comment'] = $this->name_model->filter_comment($_POST['post_id']);
		for($j=0;$j<count($data['comment']);$j++)
		{
		    $data['comment'][$j]['fb_username_com'] = $this->name_model->get_username($data['comment'][$j]['fb_id_com']);
		    $data['comment'][$j]['fb_pic_com'] = $this->name_model->get_fb_pic($data['comment'][$j]['fb_id_com']);
		    $data['comment'][$j]['total_like'] = count($this->name_model->filter_comment_like($data['comment'][$j]['com_id']));
		    $data['comment'][$j]['liked'] = $this->name_model->filter_like_fb_comment($data['comment'][$j]['com_id'], $data['fb'][0]['fb_id']);
		    
		}
		$this->load->view('load_content', $data);
	    }
	}

        function ajax_comment()
        {   
	    if($_POST)
	    {
		$com_content = $_POST['com_content'];
		$fb_id_com = $_POST['fb_id_com'];
		$fb_id_wall = $_POST['fb_id_wall'];
		$post_id = $_POST['post_id'];
		$data = array(
		    'com_content'=>$com_content,
		    'fb_id_com'=>$fb_id_com,
		    'fb_id_wall'=>$fb_id_wall,
		    'post_id'=>$post_id,
		);
		$this->name_model->insert_comment($data);
		echo "TRUE";
	    }
	    else
	    {
		echo "FAIL";
	    }
	}

        function ajax_updatecomment()
        {
	    	$fb_pic = $_POST['fb_pic'];
			$com_content = $_POST['com_content'];
			$com_time = $_POST['com_time'];
			$fb_username = $_POST['fb_username'];
			$data['data_com'] = array(
			    'com_content'=>$com_content,
			    'fb_pic'=>$fb_pic,
			    'fb_username'=>$fb_username,
			    'com_time'=>$com_time,
			);
			$this->load->view('sub_comment',$data);
	}

        function ajax_insert_likec()
        {
	    if($_POST)
	    {
		$com_id = $_POST['com_id'];
		$fb_id = $_POST['fb_id'];
		$data = array(
		    'com_id'=>$com_id,
		    'fb_id'=>$fb_id,
		);
		$this->name_model->insert_likec($data);
	    }
        }

        function ajax_delete_likec()
        {
	    if($_POST)
	    {
		$com_id = $_POST['com_id'];
		$fb_id = $_POST['fb_id'];
		$data = array(
		    'com_id'=>$com_id,
		    'fb_id'=>$fb_id,
		);
		$this->name_model->delete_likec($data);
	    }
        }


/////////////////////////////////////////comment//////////////////////////////////////////////////////

}
